package com.greenantmall.activity;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.version.IntentUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class BaseActivity extends Activity implements OnClickListener {
	public BaseActivity activity;
	private String userId;
	// NetworkDialog networkDialog = null;
	private LinearLayout linearLayout;
	private RelativeLayout noNetWork;
	private boolean canExit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.base_main);

		linearLayout = (LinearLayout) this.findViewById(R.id.linearLayoutmain);
		noNetWork = (RelativeLayout) this.findViewById(R.id.noNetWork);

		activity = this;
		userId = null;
		try {
			AVUser currentUser = AVUser.getCurrentUser();
			if (currentUser != null) {
				userId = currentUser.getObjectId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		registerReciver();
		if (IntentUtil.isConnect(this)) {
			linearLayout.setVisibility(View.VISIBLE);
			noNetWork.setVisibility(View.GONE);
		} else {
			noNetWork.setVisibility(View.VISIBLE);
			linearLayout.setVisibility(View.GONE);

		}
	}

	private void registerReciver() {
		IntentFilter downloadManagerFilter = new IntentFilter();
		downloadManagerFilter
				.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
		this.registerReceiver(netManagerReceiver, downloadManagerFilter);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		switch (keyCode) {

		case KeyEvent.KEYCODE_SEARCH:
			return true;

		case KeyEvent.KEYCODE_BACK:// 按回退键
			if (noNetWork.getVisibility() == View.VISIBLE) {
				if (canExit) {
					UtilTools.exit(this);
				} else {
					loadDataHandler.sendEmptyMessageDelayed(1002, 2000);
					canExit = true;
					Toast.makeText(this, getString(R.string.main_exit_tip),
							Toast.LENGTH_SHORT).show();
				}

				return true;
			}

		default:
			break;
		}
		return super.onKeyUp(keyCode, event);
	}

	private Handler loadDataHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1002: {
				canExit = false;
				break;
			}
			default:
				break;
			}
		}
	};

	public String getUserId() {
		return userId;
	}

	private BroadcastReceiver netManagerReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 网络连通
			if (IntentUtil.isConnect(context)) {
				linearLayout.setVisibility(View.VISIBLE);
				noNetWork.setVisibility(View.GONE);
			} else {
				noNetWork.setVisibility(View.VISIBLE);
				linearLayout.setVisibility(View.GONE);

			}
		}

	};

	protected void showError(String errorMessage) {
		showError(errorMessage, activity);
	}

	public void showError(String errorMessage, Activity activity) {
		new AlertDialog.Builder(activity)
				.setTitle(
						activity.getResources().getString(
								R.string.dialog_message_title))
				.setMessage(errorMessage)
				.setNegativeButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						}).show();
	}

	protected void onPause() {
		super.onPause();
		//AVAnalytics.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(netManagerReceiver);
	}

	protected void onResume() {
		super.onResume();
		//AVAnalytics.onResume(this);
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void setContentView(int layoutResID) {

		if (linearLayout != null) {
			LayoutInflater inflater = this.getLayoutInflater();
			View convertView = inflater.inflate(layoutResID, null);
			linearLayout.removeAllViews();
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			linearLayout.addView(convertView, params);
		}

	}

	@Override
	public void setContentView(View view) {
		if (linearLayout != null) {
			linearLayout.removeAllViews();
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			linearLayout.addView(view, params);

		}

	}
}
